package com.lucien.Practice;

import java.util.Scanner;

public class CircleArrayQueueDemo01 {
    public static void main(String[] args) {
        ArrayQueue1 arrayQueue = new ArrayQueue1(5);
        Scanner sc = new Scanner(System.in);
        char key =' ';
        System.out.println();
        boolean loop = true;
        while (loop){
            System.out.println("s(show)：显示队列");
            System.out.println("e(exit)：推出程序");
            System.out.println("a(add)：添加数据到队列");
            System.out.println("g(get)：从队列中取得数据队列");
            System.out.println("h(head)：查看队列头的数据");

            key = sc.next().charAt(0);
            switch (key){
                case 's':
                    arrayQueue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int n =sc.nextInt();
                    arrayQueue.addQueue(n);
                    break;
                case 'g':
                    try {
                        int res = arrayQueue.list();
                        System.out.printf("取出的数据是%d\n",res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        int res = arrayQueue.getHead();
                        System.out.printf("队列头部信息是%d\n",res);
                    }catch (Exception e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    sc.close();
                    loop = false;
                    break;
            }
        }
        arrayQueue.size();
        System.out.println("程序退出");
    }
}
class ArrayQueue1{
    private int maxSize;
    private int front;//指向队列第一个元素
    private int rear;//指向队列最后一个元素
    private int [] arr;

    public ArrayQueue1(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int [maxSize];
    }

    //判断队列是否满
    public boolean isFull(){
        return (rear+1)%maxSize == front;
    }

    //判断队列是否为空
    public boolean isEmpty(){
        return  rear == front;
    }

    //添加数据到队列
    public void addQueue(int n){
        if (isFull()){
            System.out.println("队列满不能添加数据");
            return;
        }
        arr[rear] = n;//直接加入数据
        rear = (rear+1)%maxSize;
    }

    //获取队列队列数据
    public int list(){
        if (isEmpty()){
            //通过抛出异常来处理
            throw new RuntimeException("队列空，不能取数据");
        }
        /** front指向队列第一个元素
         *  1、先把front对应的值保存到一个临时变量
         *  2、将front后移，考虑取模 防止越界
         *  3、将临时变量返回
         */
        int value = arr[front];
        front = (front + 1) % maxSize;
        return value;
    }

    //获取头部数据
    public int getHead(){
        if (isEmpty()){
            throw new RuntimeException("队列空，不能取数据");
        }
        return arr[front+1];
    }

    //显示所有数据
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列空，无法取得数据");
            return;
        }
        for (int i = front; i < front + size(); i++) {
            System.out.printf("arr[%d]=%d\n",i % maxSize,arr[i%maxSize]);
        }
    }

    //求出当前数列有效数据个数
    public int size(){
        return (rear + maxSize - front) % maxSize;
    }

}
